/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mamesoft.namibiamc.beans;

import com.mamesoft.namibiamc.tools.JDBC;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import hirondelle.date4j.DateTime;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author simone
 */
public class StatsBean {

    private LoginBean loginBean;
    private int vaccini;
    private int visiteSpontanee;
    private int visiteProgrammate;
    private boolean init = false;

    /** Creates a new instance of StatsBean */
    public void init() {
        DateTime d = DateTime.today(TimeZone.getDefault());

        Connection c = null;
        try {
            c = JDBC.connect();

            String q = "SELECT COUNT(*) FROM cartella "
                    + "WHERE is_fatta='T' "
                    + "AND mid='" + getLoginBean().getId() + "' "
                    + "AND data > '" + d.format("YYYY-MM-DD") + " 00:00:00'"
                    + "AND tipo_visita='spontanea'";
            ResultSet set = JDBC.executeQuery(q, c);
            if (set.next()) {
                visiteSpontanee = set.getInt(1);
            }

            q = "SELECT COUNT(*) FROM cartella "
                    + "WHERE is_fatta='T' "
                    + "AND mid='" + getLoginBean().getId() + "' "
                    + "AND data > '" + d.format("YYYY-MM-DD") + " 00:00:00'"
                    + "AND tipo_visita='vaccino'";

            set = JDBC.executeQuery(q, c);
            if (set.next()) {
                vaccini = set.getInt(1);
            }

            q = "SELECT COUNT(*) FROM cartella "
                    + "WHERE is_fatta='T' "
                    + "AND mid='" + getLoginBean().getId() + "' "
                    + "AND data > '" + d.format("YYYY-MM-DD") + " 00:00:00'"
                    + "AND tipo_visita='programmata'";

            set = JDBC.executeQuery(q, c);
            if (set.next()) {
                visiteProgrammate = set.getInt(1);
            }
            init = true;
        } catch (Exception ex) {
            Logger.getLogger(StatsBean.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (c != null) {
                    c.close();
                }
            } catch (SQLException ex) {
                Logger.getLogger(StatsBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     * @return the vaccini
     */
    public int getVaccini() {
        if (!init) {
            init();
        }
        return vaccini;
    }

    /**
     * @param vaccini the vaccini to set
     */
    public void setVaccini(int vaccini) {
        this.vaccini = vaccini;
    }

    /**
     * @return the visiteSpontanee
     */
    public int getVisiteSpontanee() {
        if (!init) {
            init();
        }
        return visiteSpontanee;
    }

    /**
     * @param visiteSpontanee the visiteSpontanee to set
     */
    public void setVisiteSpontanee(int visiteSpontanee) {
        this.visiteSpontanee = visiteSpontanee;
    }

    /**
     * @return the visiteProgrammate
     */
    public int getVisiteProgrammate() {
        if (!init) {
            init();
        }
        return visiteProgrammate;
    }

    /**
     * @param visiteProgrammate the visiteProgrammate to set
     */
    public void setVisiteProgrammate(int visiteProgrammate) {
        this.visiteProgrammate = visiteProgrammate;
    }

    /**
     * @return the loginBean
     */
    public LoginBean getLoginBean() {
        return loginBean;
    }

    /**
     * @param loginBean the loginBean to set
     */
    public void setLoginBean(LoginBean loginBean) {
        this.loginBean = loginBean;
    }
}
